core.OneLineHelper = function(aParams){
	this.init(aParams);
}

var textWidthHelper = core.newDiv(document.body, 'text_width_helper');
$(textWidthHelper).css("left", 0).css("top", "0");
core.extendClass('core.OneLineHelper', 'core.Box');

core.OneLineHelper.prototype.getTextWidth = function(aText, aClass){
	if(aClass)
		$(textWidthHelper).addClass(aClass);
	$(textWidthHelper).html(this.imagesToHTML(this.icon)+(aText || this.text));
	var result = $(textWidthHelper).width();
	if(aClass)
		$(textWidthHelper).removeClass(aClass);
	return result;
}
core.OneLineHelper.prototype.setText = function(aText){
	this.text = aText;
	this.isDirty = true;
//	$(this.textDiv).html(aText);//Images also
}


core.OneLineHelper.prototype.init = function(aParams){
	this.xType = 'oneline';
	this.superCall('init', arguments, aParams);
	this.isDirty = true;
	if(aParams.parent)
		aParams.parent.reParent(this);
	this.icon = aParams.icon;
	this.padding = aParams.padding || 5;
	this.textDiv = core.newDiv(this.div, 'oneline');
	this.align = aParams.align || 'left';
	this.setText(aParams.text);
	if(aParams.cls)
		$(this.div).addClass(aParams.cls);
	var instance = this;
	$(this.textDiv).click(function(e){
		return instance.fire('click', e);
	});
	$(this.textDiv).dblclick(function(e){
		return false;
	});
	$(this.div).click(function(e){
		return instance.fire('click', e);
	});
	$(this.div).dblclick(function(e){
		return false;
	});
	$(this.div).mousedown(function(e){
		return instance.fire('mousedown', e);
	});
	$(this.div).mouseup(function(e){
		return instance.fire('mouseup', e);
	});
	$(this.div).mouseover(function(e){
		return instance.fire('mouseover', e);
	});
	$(this.div).mouseout(function(e){
		return instance.fire('mouseout', e);
	});
	$(this.div).mousemove(function(e){
		return instance.fire('mousemove', e);
	});
};

core.OneLineHelper.prototype.reduceTextWidth = function(aDiv, aImages, aText, aWidth){
	if(this.isDirty){
		//add images to text
		var suffix = '';
		while(this.getTextWidth(aText+suffix)>aWidth){
			if(!aText){
				aText = '';
				suffix = '';
				break;
			}
			suffix = '...';			
			aText = aText.substring(0, aText.length-2);
		}
		this.reducedText = (aText || '')+suffix;
		this.isDirty = false;
	}
	$(aDiv).html(this.imagesToHTML(this.icon)+this.reducedText);
	return aDiv;
}

core.OneLineHelper.prototype.resize = function(aX, aY, aWidth, aHeight){
	this.superCall('resize', arguments, aX, aY, aWidth, aHeight);
	this.reduceTextWidth(this.textDiv, [], this.text, aWidth-2*this.padding);
//	core.log('oneline resize, height', this.text, this.instanceID, aWidth-2*this.padding, aHeight, this.align, 'middle', this.padding);
	this.alignBlock(this.textDiv, aWidth-2*this.padding, aHeight, this.align, 'middle', this.padding);
};

core.Tab = function(aParams){
	this.init(aParams);
}

core.extendClass('core.Tab', 'core.Box');

core.Tab.prototype.init = function(aParams){
	this.xType = 'tab';
	this.superCall('init', arguments, aParams);
	this.minWidth = TAB_WIDTH;
	this.menu = aParams.menu
	this.minHeight = BUTTON_HEIGHT;	
	this.tabDiv = document.createElement('div');
	this.div.appendChild(this.tabDiv);
	$(this.tabDiv).height(this.minHeight);
	$(this.tabDiv).addClass('tab');
    this.closeable = aParams.closeable;
	var instance = this;

    if(this.closeable){
        this.closeDiv = core.newDiv(this.div, 'close_tab');
        $(this.closeDiv).html('<img src="im/close.png"/>');
        $(this.closeDiv).click(function(e){
            instance.fire('close', e);
            return false;
        });
    }
	this.caption = new core.OneLineHelper({
		text: aParams.text,
		cls: 'mouse_over',
		icon: aParams.icon,
		padding: 5,
        align: aParams.align,
		parent: this
	});
//	this.reParent(this.caption);
	this.caption.on('click', function(i, e){
		var showMenu = instance==instance.parent.selectedTab && !instance.parent.dropDown;
		if(showMenu && instance.menu){
			var menu = [];
			jQuery.each(instance.menu, function(i, e){
				menu.push(e);
			});
			instance.parent.showDropDown(instance, menu, instance.parent.topDown);			
		}else{
            return instance.fire('select', e);
        }
		return false;
	});	
};

core.Tab.prototype.select = function(){
	this.selected = true;
	$(this.tabDiv).addClass('tab_selected');	
}

core.Tab.prototype.unselect = function(){
	this.selected = false;
	$(this.tabDiv).removeClass('tab_selected');	
}

core.Tab.prototype.resize = function(aX, aY, aWidth, aHeight){
	this.superCall('resize', arguments, aX, aY, aWidth, aHeight);
    var tabWidth = aWidth-2;
    if(this.closeable){
        tabWidth -= 18;
        this.alignBlock(this.closeDiv,aWidth-2, aHeight, 'right', 'middle');
    }
	$(this.tabDiv).width(aWidth-2);
	this.caption.setText(this.params.text);
	this.caption.resize(0, 0, tabWidth, aHeight);
}

core.Tabs = function(aParams){
	this.init(aParams);
};

core.extendClass('core.Tabs', 'core.Box');

core.Tabs.prototype.init = function(aParams){
	this.xType = 'tabs';
	this.superCall('init', arguments, aParams);
	this.tabs = [];
	
	this.toolbarStyle = aParams.toolbar;
	
	this.topDown = aParams.topDown || this.toolbarStyle;
	
	this.leftButton = new core.form.Button({
		icon: 'im/left.png'
	});
	this.rightButton = new core.form.Button({
		icon: 'im/right.png'
	});
	var instance = this;
	this.leftButton.on('press', function(){
		if(instance.startTab>0){
			instance.startTab--;
			instance.resize(instance.bounds.x, instance.bounds.y, instance.bounds.width, instance.bounds.height);
		}
	});
	
	this.rightButton.on('press', function(){
		if(instance.startTab+instance.tabsShow<instance.tabs.length){
			instance.startTab++;
			instance.resize(instance.bounds.x, instance.bounds.y, instance.bounds.width, instance.bounds.height);
		}
	});
	this.reParent(this.leftButton);
	this.reParent(this.rightButton);
	if(aParams.elements){
		jQuery.each(jQuery.makeArray(aParams.elements), function(i, e){
			instance.addTab(e);
		});
	}
	
	this.minWidth = TAB_WIDTH+2*(BUTTON_WIDTH_ICON+TAB_PADDING);
	this.minHeight = BUTTON_HEIGHT+2*TAB_PADDING;
	
	this.startTab = 0;
	if(this.tabs.length>0){
		this.selectTab(this.tabs[0]);
	}
};

core.Tabs.prototype.resize = function(aX, aY, aWidth, aHeight){
	this.superCall('resize', arguments, aX, aY, aWidth, aHeight);
//	core.log(this.toolbarStyle, aX, aY, aWidth, aHeight);
	this.hideDropDown();
	//Calculate tab width
	if(this.tabs.length==0)
		return;
	var tabCount = this.tabs.length;
	var effectiveWidth = aWidth-(tabCount+1)*TAB_PADDING;
	var tabWidth = Math.floor(effectiveWidth/tabCount);
	var tabsShow = tabCount;
	var showButtons = false;
	
	if(this.toolbarStyle){
		var reqWidth = TAB_PADDING;
		for(var i = 0; i<tabCount; i++){
			reqWidth += this.tabs[i].minWidth+TAB_PADDING;
		}
		if(reqWidth>aWidth){
			showButtons = true;
			effectiveWidth = aWidth - 2*BUTTON_WIDTH_ICON-TAB_PADDING;
			var width = TAB_PADDING;
			var enoughWidth = false;
			tabsShow = 0;
			for(var i = this.startTab; i<tabCount; i++){
				if(width+this.tabs[i].minWidth+TAB_PADDING>effectiveWidth){
					enoughWidth = true;
					break;
				}else{
					width += this.tabs[i].minWidth+TAB_PADDING;
					tabsShow++;
				}
				
			}
			if(!enoughWidth){
				for(var i = this.startTab-1; i>=0; i--){
					if(width+this.tabs[i].minWidth+TAB_PADDING>effectiveWidth){
						break;
					}else{
						width += this.tabs[i].minWidth+TAB_PADDING;
						tabsShow++;
						this.startTab--;
					}					
				}
			}
		}else{
			showButtons = false;
			this.startTab = 0;
		}
	}else{
		if(tabWidth>=TAB_WIDTH){
			this.startTab = 0;
		}else{
			showButtons = true;
			effectiveWidth -= 2*BUTTON_WIDTH_ICON-TAB_PADDING;
			tabsShow = Math.floor(effectiveWidth/TAB_WIDTH);
			tabWidth = Math.floor(effectiveWidth/tabsShow);
			if(this.startTab+tabsShow>=tabCount){
				this.startTab = tabCount-tabsShow;
			}
		}
	}
//	core.log('resize tabs', this.instanceID, tabCount, this.startTab, tabsShow);
	if(showButtons){
		this.leftButton.show();
		this.rightButton.show();
		this.leftButton.setDisabled(this.startTab==0);
		this.rightButton.setDisabled(this.startTab+tabsShow>=tabCount);
		this.leftButton.resize(TAB_PADDING, TAB_PADDING, BUTTON_WIDTH_ICON, BUTTON_HEIGHT);
		this.rightButton.resize(aWidth-BUTTON_WIDTH_ICON, TAB_PADDING, BUTTON_WIDTH_ICON, BUTTON_HEIGHT);
	}else{
		this.leftButton.hide();
		this.rightButton.hide();
	}
	var leftPadding = showButtons?BUTTON_WIDTH_ICON+TAB_PADDING:0;
	for(var i = 0; i<tabCount; i++){
		var tab = this.tabs[i];
		if(i<this.startTab || i>=this.startTab+tabsShow){
			tab.hide();
			continue;
		}
		leftPadding += TAB_PADDING;
		tab.show();
		
		if(this.startTab+tabsShow-1==i)
			tabWidth = aWidth-leftPadding-(showButtons?BUTTON_WIDTH_ICON:0)-TAB_PADDING;
		tab.x = leftPadding;
		if(this.toolbarStyle){
			tab.resize(leftPadding, TAB_PADDING, tab.minWidth, tab.minHeight);
			leftPadding += tab.minWidth;
		}else{
			tab.resize(leftPadding, TAB_PADDING, tabWidth, tab.minHeight);
			leftPadding += tabWidth;
		}
	}
	this.tabsShow = tabsShow;
};

core.Tabs.prototype.getDropTarget = function(aDraggable, aX, aY){
	for(var i = 0; i<this.tabs.length; i++){
		var tab = this.tabs[i];
		if(i<this.startTab || i>=this.startTab+this.tabsShow){
			continue;
		}
		var itm = tab.bounds;
		if(itm && itm.x<=aX && itm.y<=aY && itm.x+itm.width>=aX && itm.y+itm.height>=aY && tab.getDropTarget){
			return tab.getDropTarget(aDraggable, aX-itm.x, aY-itm.y);
		}
	}
	return null;
}

core.Tabs.prototype.refresh = function(){
	return this.resize(this.x, this.y, this.width, this.height);
}

core.Tabs.prototype.selectTab = function(aTab){
	this.hideDropDown();
	if(aTab!=this.selectedTab){
		if(this.selectedTab){
			this.selectedTab.unselect();
		}
		this.selectedTab = aTab;		
		this.fire('select', {}, aTab);
		this.selectedTab.select();		
	}
}

core.Tabs.prototype.removeTab = function(aTab){
	this.hideDropDown();
	
	for(var i = 0; i<this.tabs.length; i++){
		var t = this.tabs[i];
		if(t==aTab){
			this.tabs.splice(i, 1);
			aTab.hide();
			if(this.selectedTab==aTab && this.tabs.length>0){
				this.selectTab(this.tabs[i<this.tabs.length?i:i-1]);
			}
			return true;
		}
	}
	return false;
}

core.Tabs.prototype.addTab = function(aTab){
	this.tabs.push(aTab);
	this.reParent(aTab);
	var instance = this;
	aTab.on('select', function(i, e){
		if(i.instanceOf('core.Tab')){
			instance.selectTab(i);
            return false;
        }
        return true;
	});
	return this.tabs.length;
};

core.Menu = function(aParams){
	this.init(aParams);
};

core.extendClass('core.Menu', 'core.Box');

core.Menu.prototype.setElementDisabledState = function(aState, aElement){
	if(aState)
		this.disableElement(aElement);
	else
		this.enableElement(aElement);
}

core.Menu.prototype.disableElement = function(aMenuItem){
	var m = aMenuItem;
	if(!m.disabledDiv){
		m.disabled = true;
		$(m.div).removeClass('menu_item_over');
		m.disabledDiv = core.newDiv(m.control.div, 'disabled');
		$(m.disabledDiv).width(m.control.bounds.width);
		$(m.disabledDiv).height(m.control.bounds.height);
	}
}

core.Menu.prototype.enableElement = function(aMenuItem){
	var m = aMenuItem;
	if(m.disabledDiv){
		m.disabled = false;
		$(m.disabledDiv).remove();
		m.disabledDiv = null;
	}
}

core.Menu.prototype.init = function(config){
	this.superCall('init', arguments, config);
	config.parent.reParent(this);
	var instance = this;
	this.menu = config.menu || [];
	this.topDown = config.topDown;
	this.startPointX = config.startPointX;
	this.startPointY = config.startPointY;
	this.areaWidth = config.areaWidth || MENU_MIN_WIDTH;
	this.areaHeight = config.areaHeight || MENU_ITEM_HEIGHT;
	if(this.topDown)
		this.areaHeight = this.areaHeight-this.startPointY;
	else
		this.areaHeight = this.startPointY;	
	var calcMenuHeight = function(aItems){
		return aItems*(MENU_ITEM_HEIGHT+MENU_PADDING)+MENU_PADDING;
	}
	$(this.div).addClass('menu');
	this.showFrom = 1;
	this.showTo = this.menu.length-2;
	if(calcMenuHeight(this.menu.length)>this.areaHeight){
		
		this.showTo = Math.floor((this.areaHeight-MENU_PADDING)/(MENU_ITEM_HEIGHT+MENU_PADDING))-2;
		this.menu.splice(0, 0, {
			icon: 'im/up.png',
			text: 'Scroll up',
			align: 'center',
			disabled: true,
			keepOnClick: true,
			handler: function(){
				if(instance.showFrom>1){
					instance.showFrom--;
					instance.showTo--;
				}
				instance.setElementDisabledState(instance.showFrom==1, instance.menu[0]);
				instance.setElementDisabledState(instance.showTo==instance.menuCount-2, instance.menu[instance.menuCount-1]);
				instance.showFrame();
				return false;
			}
		});
		this.menu.push({
			icon: 'im/down.png',
			text: 'Scroll down',
			align: 'center',
			keepOnClick: true,
			handler: function(){
				if(instance.showTo<instance.menuCount-2){
					instance.showFrom++;
					instance.showTo++;
				}
				instance.setElementDisabledState(instance.showFrom==1, instance.menu[0]);
				instance.setElementDisabledState(instance.showTo==instance.menuCount-2, instance.menu[instance.menuCount-1]);
				instance.showFrame();				
				return false;
			}
		});
	}
	this.menuCount = this.menu.length;
	var menuWidth = 0;
	for(var i = 0; i<this.menuCount; i++){//Create menu items
		var m = this.menu[i];
		m.control = new core.OneLineHelper({
			padding: 3,
			text: m.text,
			icons: m.icons,
			parent: this,
			icon: m.icon,
			cls: 'mouse_over',
			align: m.align
		});
		m.control.menuItem = m;
		m.control.on('mouseover', function(i){
			if(!i.menuItem.disabled)
				$(i.div).addClass('menu_item_over');
		});
		
		m.control.on('mouseout', function(i){
			$(i.div).removeClass('menu_item_over');
		});
		
		m.control.on('click', function(i, e){
			if(!i.menuItem.disabled){
				var result = true;
				if(i.menuItem.handler){
					result = i.menuItem.handler(i, e);
				}
				if(result)
					result = instance.fire('select', e, i.menuItem);				
			}
			return false;
		});
		
		var itemWidth = m.control.getTextWidth()+6;
		if(itemWidth>menuWidth)
			menuWidth = itemWidth;
		m.control.hide();
	}
	menuWidth += MENU_PADDING*2;
	if(menuWidth<MENU_MIN_WIDTH)
		menuWidth = MENU_MIN_WIDTH;
	if(menuWidth>this.areaWidth)
		menuWidth = this.areaWidth;
	if(this.startPointX+menuWidth>this.areaWidth)
		this.startPointX = this.areaWidth-menuWidth;
	var menuHeight = calcMenuHeight(this.showTo-this.showFrom+3);
	$(this.div).width(menuWidth);
	$(this.div).height(menuHeight);
	$(this.div).css('left', this.startPointX);
	$(this.div).css('top', this.startPointY-(!this.topDown?menuHeight:0));
	this.menuWidth = menuWidth;
	this.showFrame();
};

core.Menu.prototype.showFrame = function(){
	this.hide();
	for(var i = 0; i<this.menuCount; i++){
		var m = this.menu[i];		
		m.control.hide();
		if(m.disabled){
			this.enableElement(m);
			m.disabled = true;
		}
	}
	var x = MENU_PADDING;
	var y = MENU_PADDING;
	this.show();
	for(var i = 0; i<this.menuCount; i++){//Create menu items
		if(i==0 || i == this.menuCount-1 || (i>=this.showFrom && i<=this.showTo)){
			var m = this.menu[i];
            m.control.show();
			m.control.resize(x, y, this.menuWidth - MENU_PADDING*2, MENU_ITEM_HEIGHT);
			if(m.disabled)
				this.disableElement(m);
			y += MENU_ITEM_HEIGHT+MENU_PADDING;			
		}
	}	
//	this.setVisibility(true);
};

core.Tabs.prototype.hideDropDown = function(){
	if(this.dropDown){
		$(this.dropDown.div).remove();
		this.dropDown = null;
	}
};

core.Tabs.prototype.showDropDown = function(aTab, aMenuArray, aTopDown){
//	console.log('Drop down menu here, ', this.parent.width, this.parent.height, this.x, this.y);
	this.dropDown = new core.Menu({
		menu: aMenuArray,
		areaWidth: this.parent.width-2*this.padding-TAB_PADDING,
		areaHeight: this.parent.height,
		startPointX: aTab.x+this.x,
		startPointY: this.y+(aTopDown?this.minHeight:0),
		parent: this.parent,
		topDown: aTopDown
	});
	var instance = this;
	this.dropDown.on('select', function(i, e, item){
		instance.hideDropDown();
		instance.selectedTab.fire('menu_select', e, instance.selectedTab, item);
	});
};
